﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;
public partial class Admin_ActivityTypeModify : System.Web.UI.Page
{
    //Page Load 
    Guid gudUserID = CommonUtility.GetLoggedOnUserId();
    protected void Page_Load(object sender, EventArgs e)
    {
        //lblErrorMessage = (Label)Master.FindControl("lblErrorMessage");
        if (!IsPostBack)
        {
            GetRegionData();
        
            try
            {
                if (Request.QueryString.Count > 0) //add
                {
                    if (Convert.ToString(Request.QueryString["lgid"])=="0")
                    {
                        lblTitle.Text = "Add Country Master";
                        ClearData();
                        LangaugeList.Visible = false;
                    }
                    else //edit
                    {

                        lblTitle.Text = "Update Country Master";
                        GetData(Convert.ToInt64(Request.QueryString["lgid"]));
                        LangaugeList.Visible = true;
                        GetCountryLanguageData(Convert.ToInt64(Request.QueryString["lgid"]));
                        GetLanguageData();
                    }
                }
            }
            catch (Exception)
            {
                Response.Redirect("~/Admin/CountryMaster.aspx");
            }
    }
    }

    //Clear Method
    private void ClearData()
    {
        
        txtActivityTypeName.Text = string.Empty;
        //txtActivityTypeCode.Text = string.Empty;
        TextBox1.Text = string.Empty;
        TextBox2.Text = string.Empty;
        regiondropdownlist.SelectedIndex = 0;
        chkIsactive.Checked = false;
        lblMessage.Text = string.Empty;
        if  (LangaugeList.Visible)
            ClearLanguageData();
    }

    private void ClearLanguageData()
    {
        hfCountryLanguageID.Value = string.Empty;
        comLanguage.SelectedIndex = 0;
        cbxLanguageActive.Checked = false;
        txtLanguageOrder.Text = string.Empty;
        btnAddLanguage.Text = "Add";
        gvLanguage.DataSource = null;
        gvLanguage.DataBind();
    }

    //reterive data in update mode
    private void GetData(Int64 intLanguageID)
    {
        CountryMasterBAL objLMBal = new CountryMasterBAL();
        CountryMasterBO objLMBO = new CountryMasterBO();
        objLMBO = objLMBal.GetCountryMaster(intLanguageID);
        
        if (objLMBO != null)
        {
            txtActivityTypeName.Text = objLMBO.CountryName;
            TextBox1.Text = objLMBO.CountryTelCode;
            TextBox2.Text = objLMBO.CountryCode;
            regiondropdownlist.SelectedValue = Convert.ToString(objLMBO.RegionID);
            //txtActivityTypeCode.Text = objLMBO.ActivityCode;
            chkIsactive.Checked = Convert.ToBoolean(objLMBO.IsActive);
        }
    }

    private DataSet GetRegion()
    {
        DataSet objDs=null;
        try
        {
            objDs = new CountryMasterBAL().GetRegionBindBAL();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog();// throw new ApplicationException(exception.Message);
        }
        return objDs;
    }
    private DataSet GetLanguage()
    {
        DataSet objDs = null;
        try
        {
            objDs = new LanugageMasterBAL().GetLanguageMaster();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog();// throw new ApplicationException(exception.Message);
        }
        return objDs;
    }

    private void GetRegionData()
    {
        
            DataSet objDs = new DataSet();
            objDs = GetRegion();
            //DataView objDv = new DataView();
            //objDv = objDs.Tables[0].DefaultView;
            //objDv.RowFilter = "CountryName  LIKE '*" + txtActivityTypeName.Text.Trim() + "*'";
            //gvLanguage.DataSource = objDv;
             regiondropdownlist.DataSource = objDs;
             regiondropdownlist.DataBind();
             regiondropdownlist.Items.Insert(0, new ListItem("-- Select --", ""));
             regiondropdownlist.SelectedIndex = 0;
    }

    private void GetCountryLanguageData(Int64 intLanguageID)
    {
        CountryMasterBAL objLMBal = new CountryMasterBAL();
        DataSet ds = objLMBal.GetCountryLanguageByCountryID (intLanguageID);
        if (ds != null && ds.Tables.Count>0 )
        {
            gvLanguage.DataSource = ds;
            gvLanguage.DataBind();
        }
    }
    private void GetLanguageData()
    {

        DataSet objDs = new DataSet();
        objDs = GetLanguage();
        //DataView objDv = new DataView();
        //objDv = objDs.Tables[0].DefaultView;
        //objDv.RowFilter = "CountryName  LIKE '*" + txtActivityTypeName.Text.Trim() + "*'";
        //gvLanguage.DataSource = objDv;
        comLanguage.DataSource = objDs;
        comLanguage.DataBind();
        comLanguage.Items.Insert(0, new ListItem("-- Select --", ""));
        comLanguage.SelectedIndex = 0;
    }


    //save button event
    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //lblMessage.Text = "";
            if (Request.QueryString.Count > 0)
            {
               
                CountryMasterBAL objLMBal;
                CountryMasterBO objLMBO;
                //Guid gudUserID = new Guid(Convert.ToString(Session["UserID"]));
                int intLgid = Convert.ToInt16(Request.QueryString["lgid"]);
                string strmod = Convert.ToString(Request.QueryString["mod"]);
                string strReturnValue;

                if (strmod == "in")//add
                {
                    objLMBal = new CountryMasterBAL();
                    objLMBO = new CountryMasterBO();

                    objLMBO.SetMethod = "INSERT";
                    objLMBO.CountryID = 0;
                    objLMBO.CountryName = txtActivityTypeName.Text.Trim();
                    //objLMBO.ActivityCode = txtActivityTypeCode.Text.Trim();
                    objLMBO.CountryTelCode =TextBox1.Text.Trim();
                    objLMBO.CountryCode =TextBox2.Text.Trim();
                    objLMBO.RegionID = Convert.ToInt32(regiondropdownlist.SelectedValue);
                    objLMBO.CreatedBy = gudUserID;
                    objLMBO.ModifyBy = gudUserID;
                    objLMBO.ModifyDate = System.DateTime.Today;
                    objLMBO.IsActive = Convert.ToBoolean(chkIsactive.Checked);
                    strReturnValue = (string)objLMBal.SetCountryMaster(objLMBO.SetMethod, objLMBO.CountryID, objLMBO.CountryName, objLMBO.RegionID, objLMBO.CountryTelCode, objLMBO.CountryCode, objLMBO.CreatedBy, objLMBO.@ModifyBy, objLMBO.ModifyDate, objLMBO.IsActive);
                    if (strReturnValue == "SUCCESS")
                    {
                        ClearData();
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record has been save succesfully.');", true);
                        //lblMessage.Text = "Record has been save succesfully.";
                        //lblMessage.ForeColor = System.Drawing.Color.Green;

                    }
                    else if (strReturnValue == "EXIST")
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Country type already exists.');", true);
                        //lblMessage.Text = "Country type already exists."; //EnrollResources.GetMessage(Message.InsertMessage); //  
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
                        //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                }
                else if (strmod == "up")
                {

                    objLMBal = new CountryMasterBAL();
                    objLMBO = new CountryMasterBO();

                    objLMBO.SetMethod = "UPDATE";
                    objLMBO.CountryID = intLgid;
                    objLMBO.CountryName = txtActivityTypeName.Text.Trim();
                    //objLMBO.ActivityCode = txtActivityTypeCode.Text.Trim();
                    objLMBO.CountryTelCode = TextBox1.Text.Trim(); ;
                    objLMBO.CountryCode = TextBox2.Text.Trim();
                    objLMBO.RegionID = Convert.ToInt32(regiondropdownlist.SelectedValue);
                    objLMBO.ModifyBy = gudUserID;
                    objLMBO.ModifyDate = System.DateTime.Today;
                    objLMBO.IsActive = Convert.ToBoolean(chkIsactive.Checked);
                    strReturnValue = (string)objLMBal.SetCountryMaster(objLMBO.SetMethod, objLMBO.CountryID, objLMBO.CountryName, objLMBO.RegionID, objLMBO.CountryTelCode, objLMBO.CountryCode, objLMBO.CreatedBy, objLMBO.@ModifyBy, objLMBO.ModifyDate, objLMBO.IsActive);
                    if (strReturnValue == "SUCCESS")
                    {
                        //ClearData();
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record has been updated succesfully.');", true);
                        //lblMessage.Text = "Record has been updated succesfully.";
                        //lblMessage.ForeColor = System.Drawing.Color.Green;

                    }
                    else if (strReturnValue == "EXIST")
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Activity Type already exists.');", true);
                        //lblMessage.Text = "Activity Type already exists."; //EnrollResources.GetMessage(Message.InsertMessage); // 
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
                        //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                }

            }
        }
    }

    //cancel button event
    protected void btnCancel_OnClick(object sender, EventArgs e)
    {
          if (Request.QueryString.Count > 0) //add
                {
                    if (Convert.ToString(Request.QueryString["lgid"]) == "0")
                    {
                        ClearData();
                        Response.Redirect("~/Admin/CountryMaster.aspx");
                    }
                    else
                    {
                        lblMessage.Text = string.Empty;
                        Response.Redirect("~/Admin/CountryMaster.aspx");
                        GetData(Convert.ToInt64(Request.QueryString["lgid"]));
                    }

            }
    }



    protected void gvLanguage_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string[] arg = new string[4];
        arg = e.CommandArgument.ToString().Split(';');
        if (e.CommandName == "EDITLANGUAGE")
        {
            //Eval("CountryLanguageID") + ";" + Eval("LanguageID") + ";" + Eval("IsActive") + ";" + Eval("LanguageOrder")
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Edit record. ');", true);
            hfCountryLanguageID.Value = arg[0];
            comLanguage.SelectedValue = arg[1];
            cbxLanguageActive.Checked = Convert.ToBoolean(arg[2]);
            txtLanguageOrder.Text = arg[3];
            btnAddLanguage.Text = "Update";
        }
    }

    protected void btnAddLanguage_Click(object sender, EventArgs e)
    {
        CountryMasterBAL objCMBal = new CountryMasterBAL();
        if (Page.IsValid)
        {   
            if (btnAddLanguage.Text == "Add")
            {
                string strReturnValue = (string)objCMBal.SetCountryLangaugeMaster("INSERT", null, comLanguage.SelectedValue, Convert.ToString(Request.QueryString["lgid"]), Convert.ToBoolean(cbxLanguageActive.Checked), txtLanguageOrder.Text, gudUserID,null);

                if (strReturnValue == "SUCCESS")
                {
                    ClearLanguageData();
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record has been save succesfully.');", true);
                    //lblMessage.Text = "Record has been save succesfully.";
                    //lblMessage.ForeColor = System.Drawing.Color.Green;
                    GetCountryLanguageData( Convert.ToInt64(Request.QueryString["lgid"]));

                }
                else if (strReturnValue == "EXIST")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Language already exists in Country.');", true);
                    //lblMessage.Text = "Country type already exists."; //EnrollResources.GetMessage(Message.InsertMessage); //  
                    //lblMessage.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
                    //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                    //lblMessage.ForeColor = System.Drawing.Color.Red;
                }
            }
            else if(btnAddLanguage.Text == "Update")
            {
                if (Convert.ToInt32(hfCountryLanguageID.Value) > 0)
                {
                    string strReturnValue = (string)objCMBal.SetCountryLangaugeMaster("UPDATE", Convert.ToInt32(hfCountryLanguageID.Value), comLanguage.SelectedValue, Convert.ToString(Request.QueryString["lgid"]), Convert.ToBoolean(cbxLanguageActive.Checked), txtLanguageOrder.Text, null, gudUserID);
                    if (strReturnValue == "SUCCESS")
                    {
                        ClearLanguageData();
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record has been save succesfully.');", true);
                        //lblMessage.Text = "Record has been save succesfully.";
                        //lblMessage.ForeColor = System.Drawing.Color.Green;
                        GetCountryLanguageData(Convert.ToInt64(Request.QueryString["lgid"]));

                    }
                    else if (strReturnValue == "EXIST")
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Language already exists in Country.');", true);
                        //lblMessage.Text = "Country type already exists."; //EnrollResources.GetMessage(Message.InsertMessage); //  
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
                        //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                        //lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                }
            }
        }
    }

    protected void btnClearLanguge_Click(object sender, EventArgs e)
    {

    }
}